package com.yanggu.flink.datastream_api.wordcount

import org.apache.flink.api.scala._

object WordCount {

  def main(args: Array[String]): Unit = {

    //创建执行环境
    val environment = ExecutionEnvironment.getExecutionEnvironment

    //从文件中读取数据

    val inputDS = environment.readTextFile(getClass.getResource("/hello.txt").getPath)

    //先进行分组, 然后转换成元祖, 接着进行分组, 最后进行聚合计算
    val wordCountDS = inputDS
      .flatMap(_.split(" "))
      .map((_, 1))
      .groupBy(0)
      .sum(1)

    //打印输出
    wordCountDS.print()
  }

}
